<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
</html>

<script>

    (function(){
        function Observer(){
            this.obj = {};
            this.on = function(type,handler){
                this.obj[type] = (this.obj[type] ? this.obj[type] : []);
                this.obj[type].push(handler);
            }

            this.emit = function(type,msg){
                if(this.obj[type]){
                    this.obj[type].forEach((item) => {
                        item(msg);
                    });
                }
            }
        }
        window.Observer = Observer;
    })()

    let oo = new Observer();

    //订阅
    oo.on('xx',function(msg){console.log("xxx xx xx " + msg);})
    //发布
    oo.emit('xx','xxoo');

</script>